package com.kaoshi.controller;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kaoshi.common.config.Result;
import com.kaoshi.entity.Doctor;
import com.kaoshi.entity.Team;
import com.kaoshi.service.IDoctorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2024-10-29
 */
@RestController
@RequestMapping("/doctor")
@CrossOrigin
public class DoctorController {
    @Autowired
    private IDoctorService doctorService;
    @RequestMapping("findAll")
    public Result findAll(Integer current, Integer pageSize, @RequestBody Doctor doctor){
        LambdaQueryWrapper<Doctor> qw = new LambdaQueryWrapper<>();
        if (doctor.getIid()!=null){
            qw.eq(Doctor::getIid,doctor.getIid());
        }
        if (doctor.getTid()!=null){
            qw.eq(Doctor::getTid,doctor.getTid());
        }
        if (doctor.getName()!=null&&doctor.getName()!=""){
            qw.like(Doctor::getName,doctor.getName());
        }
        qw.eq(Doctor::getDel,1);
        Page<Doctor> p = new Page<>(current,pageSize);
        return Result.ok(doctorService.page(p,qw));
    }
    @RequestMapping("save")
    public Result save(@RequestBody Doctor doctor) {
        if (doctor.getId() != null && !doctor.getId().equals("")) {
            return Result.ok(doctorService.updateById(doctor));
        } else {
            doctor.setCreation(LocalDateTime.now());
            return Result.ok(doctorService.save(doctor));
        }
    }
    @RequestMapping("dc")
    public void dc(HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("学生列表", "UTF-8").replaceAll("/+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        //2.查询要导出的数据
        List<Doctor> studentList= doctorService.list();
        for (Doctor institution : studentList) {

            String introduce = institution.getImg();
//去除图片中数据的前缀
            String replace = introduce.replace("https://spyx-h2310a.oss-cn-beijing.aliyuncs.com/", "");
//3.更改oss桶
            byte[] imgBytes = OssImageUtils.getImageBytesFromOss("spyx-h2310a", replace);
            institution.setImgBytes(imgBytes);

        }
        //3.使用EasyExcel完成导出
        EasyExcel.write(response.getOutputStream())
                .head(Doctor.class)
                .excelType(ExcelTypeEnum.XLSX)
                .sheet("学生列表")
                .doWrite(studentList);
    }
    @RequestMapping("ztupdate")
    public Result ztupdate(@RequestBody Doctor doctor){
//        Doctor d = new Doctor();
//        d.setId(doctor.getId());
//        d.setStatus(doctor.getStatus());
//        if (d.getStatus()==0){
//            d.setStatus(0);
//        }else {
//            d.setStatus(1);
//        }
        doctorService.updateById(doctor);
        return Result.ok("修改成功！");
    }

    @RequestMapping("getDoctor")
    public List<Doctor> getDoctor(){
        return doctorService.list();
    }
}
